package practice_2025_9.practice_9_19;

import practice_2025_9.practice_9_5.ListNode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    /**
     * 删除链表中的重复元素
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        Map<Integer, Integer> hash = new HashMap<>();
        ListNode cur = head;
        while(cur != null) {
            hash.put(cur.val, hash.getOrDefault(cur.val, 0) + 1);
            cur = cur.next;
        }
        ListNode newHead = new ListNode();
        ListNode newCur = newHead;
        cur = head;
        while(cur != null) {
            if (hash.get(cur.val) <= 1) {
                newCur.next = cur;
                newCur = newCur.next;
            }
            cur = cur.next;
        }
        newCur.next = null;
        return newHead.next;
    }

    /**
     * 删除链表中的重复元素2
     * @param head
     * @return
     */
    public ListNode deleteDuplicates2(ListNode head) {
        if (head == null) {
            return head;
        }

        ListNode newHead = new ListNode(0, head);
        ListNode cur = newHead;
        while(cur.next != null && cur.next.next != null) {
            if (cur.next.val == cur.next.next.val) {
                int key = cur.next.val;
                while(cur.next != null && cur.next.val == key) {
                    cur.next = cur.next.next;
                }
            } else {
                cur = cur.next;
            }
        }
        return newHead.next;
    }

    /**
     * 验证 IP 地址
     * @param queryIP
     * @return
     */
    public String validIPAddress(String queryIP) {
        if (queryIP == null) {
            return "Neither";
        }
        if (queryIP.contains(".")) {
            return validIpv4(queryIP);
        } else if (queryIP.contains(":")) {
            return validIpv6(queryIP);
        } else {
            return "Neither";
        }
    }
    // 判断是否是 Ipv4 地址
    public String validIpv4(String queryIP) {
        // 判断前后是否有 .
        int len = queryIP.length();
        if (queryIP.charAt(0) == '.' || queryIP.charAt(len - 1) == '.') {
            return "Neither";
        }
        int i = 0;
        int count = 0;
        while(i < len) {
            if (count > 4 || queryIP.charAt(i) == '.') {
                return "Neither";
            }
            // 计算数字
            int num = 0;
            int sz = 0;
            // System.out.println("i: " + i + " " + (i < len));
            int k = i;
            while(k < len && queryIP.charAt(k) != '.') {
                char ch = queryIP.charAt(k);
                if (k - i <= 3 && ch >= '0' && ch <= '9' ) {
                    num = num * 10 + (ch - '0');
                } else {
                    return "Neither";
                }
                k++;
            }
            // 判断数字
            // System.out.println("k: " + k + "")
            if (num > 255 || (k - i > 1 && queryIP.charAt(i) == '0')) {
                return "Neither";
            }
            i = k;
            count++;
            // 跳过 .
            i++;
        }
        return count == 4 ? "IPv4":  "Neither";
    }
    // 判断是否是 Ipv6 地址
    public String validIpv6(String queryIP) {
        // 判断前后是否有 :
        int len = queryIP.length();
        if (queryIP.charAt(0) == ':' || queryIP.charAt(len - 1) == ':') {
            return "Neither";
        }
        int i = 0;
        int count = 0;
        while(i < len) {
            if (count > 8 || queryIP.charAt(i) == ':') {
                return "Neither";
            }
            int k = i;
            while(k < len && queryIP.charAt(k) != ':') {
                char ch = queryIP.charAt(k);
                if ((k - i) > 4 || !((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))) {
                    return "Neither";
                }
                k++;
            }
            if (k - i > 4) {
                return "Neither";
            }
            i = k;
            count++;
            i++;
        }
        return count == 8? "IPv6" : "Neither";
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.validIPAddress("256.256.256.256");
    }
}